package com.qztc.news_end_core.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;

import com.qztc.news_end_dto.dto.Comment;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/comment")
@Tag(name = "新闻评论接口")
public class CommentController {

    @Operation(summary = "新增新闻评论")
    @PostMapping("/add")
    public boolean addComment(@RequestBody Comment comment) {
        comment.setCreatedate(new Date().toLocaleString());
        return comment.insert();
    }

    @Operation(summary = "查询当前新闻下的所有评论")
    @GetMapping("/getAll")
    public List<Comment> getAllComment(@RequestParam Integer newsid) {
        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        wrapper.eq("newsid", newsid);
        String orderSql = "ORDER BY " +
                "CASE " +
                "WHEN createdate REGEXP '上午' THEN STR_TO_DATE(createdate, '%Y年%m月%d日 上午%h:%i:%s') " +
                "WHEN createdate REGEXP '下午' THEN STR_TO_DATE(createdate, '%Y年%m月%d日 下午%h:%i:%s') " +
                "ELSE STR_TO_DATE(createdate, '%Y年%m月%d日 %h:%i:%s') " +
                "END " +
                "DESC";
        wrapper.last(orderSql);
        return new Comment().selectList(wrapper);
    }

    @Operation(summary = "根据新闻评论id删除评论")
    @DeleteMapping("/deletebycommentid")
    public boolean deleteCommentById(@RequestBody Comment comment) {
        return comment.deleteById();
    }

    @Operation(summary = "查询所有评论")
    @GetMapping("/get")
    public List<Comment> getComment() {
        return new Comment().selectAll();
    }
}
